<?php
namespace Course\Controller;
use Common\Controller\AdminbaseController;

use Think\Controller;

class FilesController extends Controller{
	public function excelfiles(){
		if($_FILES['file']['error'] == 0){
			$type = trim($_FILES['file']['type']);

			if($type == 'application/vnd.ms-excel' || $type == 'application/octet-stream'){
                if($_FILES['file']['size'] <= 5000000){
                    $name = $_FILES['file']['name'];
                    $name_arr = explode('.', $name);
                    $new_name = time().uniqid().'.'.$name_arr[count($name_arr)-1];
                    $path = 'data/upload/excel/'.date('Y',time()).date('m',time()).date('d',time());
                    $new_path = $path.'/'.$new_name;

                    if(!is_dir($path)){
                        mkdir($path,0777,true);
                    }
                    if(move_uploaded_file($_FILES['file']['tmp_name'],$new_path)){
                        ob_end_clean();
                        $temp=file($new_path);
                        $data = eval('return '.iconv('gbk','utf-8',var_export($temp,true)).';');
                        for($i=0;$i <count($data);$i++){
                            if($i == 0){
                                $keyarr = explode(',',$data[0]);
                            }
                            if($i > 0){
                                $daan = explode(',', $data[$i]);
                                for($j=0;$j<count($keyarr);$j++){
                                    $uparr[$keyarr[$j]] = $daan[$j];
                                }
                                $updata[] = $uparr;
                            }
                        }
                        foreach($updata as $v){
                            if($v['typeid'] != ''){
                                $add = M('exam_shiti')->add($v);
                            }
                        }
                        if($add){
                            echo 1;die;
                        }
                    }else{
                        echo "<script>alert('上传文件失败！')</script>";die;
                    }
                }else{
                    echo "<script>alert('上传文件过大！')</script>";die;
                }
			}else{
                echo "<script>alert('只能导入csv文件类型！')</script>";die;
            }
//          $type_array = array('image/jpeg','image/pjpeg','image/gif','image/png','application/octet-stream');

        }
	}
	public function pdffiles(){
		$i = 0;
        $length = count($_FILES['file']['name']);
        $img = array();
        for($i;$i<$length;$i++){    //接收过来的东西， 处理一下
            $img[$i]['name'] = $_FILES['file']['name'][$i];
            $img[$i]['type'] = $_FILES['file']['type'][$i];
            $img[$i]['tmp_name'] = $_FILES['file']['tmp_name'][$i];
            $img[$i]['error'] = $_FILES['file']['error'][$i];
            $img[$i]['size'] = $_FILES['file']['size'][$i];
        }
		foreach($img as $vv){
			if($vv['type'] != 'application/pdf'){
				$error = 1;
			}
		}
		if($error == 1){
			echo "<script>alert('只能上传PDF文件类型！')</script>";die;
		}
//		print_r($img);die;
		foreach ($img as $v){    //循环上传
            if($v['error'] == 0){
//              $type_array = array('image/jpeg','image/pjpeg','image/gif','image/png','application/octet-stream');
                if($v['size'] <= 5000000){
                    $name = $v['name'];
                    $name_arr = explode('.', $name);
                    $new_name = time().uniqid().'.'.$name_arr[count($name_arr)-1];
                    $path = 'data/upload/undefined/'.date('Y',time()).date('m',time()).date('d',time());
                    $new_path = $path.'/'.$new_name;

                    if(!is_dir($path)){
                        mkdir($path,0777,true);
                    }
                    if(move_uploaded_file($v['tmp_name'],$new_path)){
                        $ppp .= '/'.$new_path.',';   //循环里面用逗号拼接一下字符串
                    }else{
                        echo "<script>alert('上传文件失败！')</script>";die;
                    }
                }else{
                    echo "<script>alert('上传文件过大！')</script>";die;
                }
            }
        }
        if($ppp){    //返回上传后的路劲
        	if(substr($ppp,-1) == ','){
        		echo substr($ppp,0,-1);die;
        	}else{
        		echo $ppp;die;
        	}
            
        }
    }
	
	

	
}